package com.clound.order.controller;


import com.clound.product.client.ProductClient;
import com.clound.product.common.DecreaseStockInput;
import com.clound.product.common.ProductInfoOutput;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * Created by zzy on 2018/4/8.
 */
@RestController
public class ClientController {

    private final static Logger log= LoggerFactory.getLogger(ClientController.class);

/*    @Autowired
    private LoadBalancerClient loadBalancerClient;*/

/*    @Autowired
    private RestTemplate restTemplate;*/

    /**
     *
     */
   @Autowired
   private ProductClient productClient;

    /**
     * 1、RestTemplate
     * 2、FeignClient
     * 都是用ribbon做负载均衡
     * @return
     */
    @GetMapping("/getProductMsg")
    public String getProductMsg(){
        //1、第一种方式
/*        RestTemplate restTemplate1=new RestTemplate();
        String response=  restTemplate1.getForObject("http://localhost:8080/msg",String.class);*/ //单个地址，写死IP

        //2、第二种方式 loadBalancerClient通过服务ID获取服务信息
/*        ServiceInstance serviceInstance= loadBalancerClient.choose("PRODUCT");
        String requestUrl=String.format("http://%s:%s",serviceInstance.getHost(),serviceInstance.getPort())+"/msg";
        response=  restTemplate1.getForObject(requestUrl,String.class); //*/

        //3、第三种方式(利用@LoadBalanced)
        //response=  restTemplate.getForObject("http://PRODUCT/msg",String.class);

        //log.info(response);
        //String response=productClient.productMsg();
        return null;
    }

    @GetMapping("/listForOrder")
    public List<ProductInfoOutput> listForOrder(){
        List<ProductInfoOutput> result=productClient.listForOrder(Arrays.asList("157875196366160022","157875227953464068"));
        return result;
    }
    @GetMapping("/decreaseStock")
    public void decreaseStock(){
        DecreaseStockInput cartDTO=new DecreaseStockInput("157875196366160022",3);
        productClient.decreaseStock(Arrays.asList(cartDTO));
    }
}
